
;Chinmaynanda Das, 
;Dhaneswar Rath Institute of Engineering and Manegment Studies, Cuttack
;---------------------------------------------------------

;ADC inputs
;IN4 - LM35
;IN5 - MOISTURE SENSOR
;IN6 - HIH4000
;IN7 - LIGHT SENSOR
;ADD_C  Connected to Vcc
;---------------------------------------------------------

; Microcontroller connections to LCD.

RS 	BIT	P2.5		; P2.5 is connected to RS pin of LCD
RW 	BIT	P2.6		; P2.6 is connected to R/W pin of LCD
E 	BIT	P2.7		; P2.5 is connected to E pin of LCD

LCDDATA	equ	P0	; P0.0-P0.7 are connected to LCD data pins D0-D7

SWITCH	equ	P3.2	; P3.2 (INT0) is connected to SWITCH for LCD

; Microcontroller connections to ADC0808/9 lines.

OE		EQU	P3.0	; Pin 9 Output Enable
START	EQU	P3.1	; Pin 6 Start
EOC		EQU	P3.3	; Pin 7 EOC
ADD_A	EQU	P3.4	; Pin 25 ADD A
ADD_B	EQU	P3.5	; Pin 24 ADD B
ALE		EQU	P3.7 	; Pin 22 ALE

ADCDATA	EQU	P1		; Data Lines


; Microcontroller connections to Actuators

COOLER	EQU	P2.4
PUMP	EQU	P2.3
SPRAYER	EQU	P2.2
LIGHT1	EQU	P2.1
LIGHT2	EQU	P2.0
BUZZER	EQU	P3.6
;---------------------------------------------------------

; Register definitions.

LCD_INTR	BIT	00h		;LCD Interrupt flag bit

BUZZ 		BIT 01h 		;Buzzer flag
BUZZ1 		BIT 02h 		;Buzzer flag
BUZZ2 		BIT 03h 		;Buzzer flag
BUZZ3 		BIT 04h 		;Buzzer flag
T_BUFFER	EQU	30h		;Temperature buffer (ADC o/p)
M_BUFFER	EQU	31h		;Soil Moisture buffer (ADC o/p)
RH_BUFFER	EQU	32h		;RH buffer (ADC o/p)
L_BUFFER	EQU	33h		;Light buffer (ADC o/p)
LIGHT_LEVEL	EQU	34h		;Buffer to store light level
MOIST_LEVEL	EQU 35h		;Buffer to store moisture level

 
;----------------------------------------------------------

	org 00h
	
	LJMP main

	org 03h
	
	LJMP int_isr	; Interrupt for the LCD switch

	org 07h

				
main:
   	CLR 0A0h 		;Clear Port 2 for the actuators to turn off
	CLR 0B0h 		;Clear port 3 for the buzzer to turn off
	SETB BUZZER
	LCALL DELAY
	CLR BUZZER 		;Turn off buzzer
	
	MOV 60h, #'C'		; Intitialising Actuator status message	
	MOV 61h, #':'
	MOV 62h, #' '
	MOV 63h, #'O'
	MOV 64h, #'F' 
	MOV 65h, #'F'

	MOV 66h, #' '
	MOV 67h, #' '

	MOV 68h, #'P'
	MOV 69h, #'U'
	MOV 6Ah, #':'
	MOV 6Bh, #'O'
	MOV 6Ch, #'F'
	MOV 6Dh, #'F'
	MOV 6Eh, #0

	MOV 70h, #'S'
	MOV 71h, #'P'
	MOV 72h, #':'
	MOV 73h, #'O'
	MOV 74h, #'F'
	MOV 75h, #'F'
	
	MOV 76h, #' '
	MOV 77h, #' '
	
	MOV 78h, #'L'
	MOV 79h, #'I'
	MOV 7Ah, #':'
	MOV 7Bh, #'O'
	MOV 7Ch, #'F'
	MOV 7Dh, #'F'
	MOV 7Eh, #0

	;Main Code
	;-----------------------------------------------------------------------
	
	MOV TMOD, #01h	;Timer 0 Mode 1, 16 bit timer (for generating 5 second delay for LCD)
	SETB TCON.0		;Make INT0 edge triggered
	MOV IE, #81h	;Enable INT0
	CLR LCD_INTR	;Make LCD interrupt flag =0
	ACALL lcd_init	;LCD initialisation
	ACALL line_1	;Display welcome message
back:
	ACALL adcread		;Read sensor data from ADC
	ACALL data_display	;call routine to display to LCD
	ACALL threshold_check	;call routine to check threshold
	SJMP back			;repeat ADC process


lcd_init:			;LCD initialisation
	MOV A, #38H		;init. LCD 2 lines,5x7 matrix (CHANGE THIS!!!)
    ACALL comnwrt   	;call command subroutine
    ACALL lcddelay  	;give LCD some time
    MOV A, #0EH     	;display on, cursor on
    ACALL comnwrt   	;call command subroutine
    ACALL lcddelay  	;give LCD some time
    MOV A, #01      	;clear LCD
    ACALL comnwrt   	;call command subroutine
    ACALL lcddelay  	;give LCD some time
    MOV A, #06H     	;shift cursor right
    ACALL comnwrt   	;call command subroutine
    ACALL lcddelay  	;give LCD some time
	RET      

line_1:				;Displaying initialisation message
	MOV A, #080H   		;cursor at line 1, position 0
   	ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init1	;address of line 1 of initialisation message
repeat1:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_2
	ACALL datawrt		;call data display routine
	ACALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat1

	
line_2:
	MOV A, #0C0h   		;cursor at line 2, position 0
    ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	
	MOV DPTR, #init2	;address of line 2 of initialisation message
repeat2:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_3
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat2		;jump to label repeat2	


line_3:				;Displaying initialisation message
	MOV A, #080H   		;cursor at line 1, position 0
   	ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init3	;address of line 1 of initialisation message
repeat3:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_4
	ACALL datawrt		;call data display routine
	ACALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat3

line_4:
	MOV A, #0C0h   		;cursor at line 2, position 0
    ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init4	;address of line 2 of initialisation message
repeat4:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_5
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat4		;jump to label repeat4

line_5:				;Displaying initialisation message
	MOV A, #080H   		;cursor at line 1, position 0
   	ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init5	;address of line 1 of initialisation message
repeat5:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_6
	ACALL datawrt		;call data display routine
	ACALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat5

line_6:
	MOV A, #080H   		;cursor at line 1, position 0
    ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init6	;address of line 2 of initialisation message
repeat6:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ line_7
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC DPTR		;increment address
	SJMP repeat6		;jump to label repeat4
line_7:
	MOV A, #0C0h   		;cursor at line 2, position 0
    ACALL comnwrt   	;call command subroutine
	ACALL lcddelay  	;give LCD some time
	MOV DPTR, #init7	;address of line 2 of initialisation message
repeat7:
	CLR A
	MOVC A, @A+DPTR		;moving the data to accumulator
	JZ exit
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC DPTR			;increment address
	SJMP repeat7		;jump to label repeat7
exit:
	MOV  A, #01h		;clear LCD
	LCALL comnwrt
	LCALL   lcddelay	;give LCD some time
	RET

comnwrt:                ;send command to LCD
	MOV LCDDATA, A		;copy reg A to port1
	CLR RS			;RS=0 for command
	CLR RW			;R/W=0 for write
	SETB E			;E=1 for high pulse
	MOV R3,#50
REG_3:	
	MOV R4,#255
	DJNZ R4,$
	DJNZ R3,REG_3
	CLR E			;E=0 for H-to-L pulse 
	RET

datawrt:                ;write data to LCD
    MOV LCDDATA, A    	;copy reg A to port1 
    SETB RS            	;RS=1 for data
    CLR RW      		;R/W=0 for write
    SETB E				;E=1 for high pulse
	MOV R3,#50
REG_4:
	MOV R4,#255
	DJNZ R4,$
	DJNZ R3,REG_4
	
    CLR E			;E=0 for H-to-L pulse
   	RET    


lcddelay:			;Delay routine for LCD
	MOV R3, #50   
here2:
	MOV R4, #255  
here3:
	DJNZ R4, here3 		;stay until R4 becomes 0
	DJNZ R3, here2
    RET
	

adcread:			;Initializing ADC
	;SENSOR 1
	MOV ADCDATA, #0FFH	; Data lines for input
	SETB EOC			; Make EOC i/p
	ACALL delay
	CLR ALE				; clearing ALE
	CLR START			; Make start high
	CLR OE				; Disable o/p
	CLR ADD_A			;A=0
	CLR ADD_B			;B=0	;Select IN4
	ACALL delay	
	SETB ALE			;latching the address
	ACALL delay
	SETB START			;start conversion pulse
	ACALL delay
	CLR ALE				;ALE H-L transition
	CLR START			;START H-L transition
	
    	JB EOC,$
	JNB EOC,$ 
	ACALL delay
	SETB OE				;enable o/p
	ACALL delay		
	MOV T_BUFFER, ADCDATA	;store adc data to buffer
	CLR OE				;disable o/p
	
	;SENSOR 2
	MOV ADCDATA, #0FFH	; Data lines for input
	SETB EOC		; Make EOC i/p
	ACALL delay
	CLR ALE			; clearing ALE
	CLR START		; Make start high
	CLR OE			; Disable o/p
	SETB ADD_A		;A=1
	CLR ADD_B		;B=0	Select IN5
	ACALL delay	
	SETB ALE		;latching the address
	ACALL delay
	SETB START		;start conversion pulse
	ACALL delay
	CLR ALE			;ALE H-L transition
	CLR START		;START H-L transition
	
    	JB EOC,$
	JNB EOC,$ 
	ACALL delay
	SETB OE			;enable o/p
	ACALL delay		;
	MOV M_BUFFER, ADCDATA	;store adc data to buffer
	CLR OE			;disable o/p
	
	;SENSOR 3
	MOV ADCDATA, #0FFH	; Data lines for input
	SETB EOC		; Make EOC i/p
	ACALL delay
	CLR ALE			; clearing ALE
	CLR START		; Make start high
	CLR OE			; Disable o/p
	CLR ADD_A		;A=0
	SETB ADD_B		;B=1	Select IN6
	ACALL delay	
	SETB ALE		;latching the address
	ACALL delay
	SETB START		;start conversion pulse
	ACALL delay
	CLR ALE			;ALE H-L transition
	CLR START		;START H-L transition
	
    	JB EOC,$
	JNB EOC,$
	ACALL delay
	SETB OE			;enable o/p
	ACALL delay	
	MOV RH_BUFFER, ADCDATA	;store adc data to buffer
	CLR OE			;disable o/p
	
	;SENSOR 4
	MOV ADCDATA, #0FFH	; Data lines for input
	SETB EOC		; Make EOC i/p
	ACALL delay
	CLR ALE			; clearing ALE
	CLR START		; Make start high
	CLR OE			; Disable o/p
	SETB ADD_A		;A=1
	SETB ADD_B		;B=1	Select IN7
	ACALL delay	
	SETB ALE		;latching the address
	ACALL delay
	SETB START		;start conversion pulse
	ACALL delay
	CLR ALE			;ALE H-L transition
	CLR START		;START H-L transition
	
    	JB EOC,$
	JNB EOC,$
	ACALL delay
	SETB OE			;enable o/p
	ACALL delay	
	MOV L_BUFFER, ADCDATA	;store adc data to buffer
	CLR OE			;disable o/p
	
	RET
delay:			;Delay subroutine 
	MOV R3, #250
here1:
	MOV R4, #255
here:	
	DJNZ R4, here
	DJNZ R3, here1
	RET


data_display:			;LCD routine to display sensor and actuator data

	JNB LCD_INTR, sensor_display	;if ACT=0, jump to display sensor data label
	MOV  A, #01h		;clear LCD
	ACALL comnwrt
	ACALL lcddelay		;give LCD some time
	MOV  A,#80H       	;cursor at line 1, position 0
    ACALL comnwrt     	;call command subroutine
	ACALL lcddelay      	;give LCD some time
	MOV R0, #60h		;address of display message line 1
line1:
	CLR A
	MOV A, @R0		;moving the data to accumulator
	JZ nxt_line		;exit loop on completion of display and start display of LCD next line
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC R0			;increment address
	SJMP line1		;repeat display till end of message
       	
 nxt_line:			;Displaying on line 2 of LCD
	MOV     A, #0C0h    	;cursor at line 2, position 0
 	LCALL   comnwrt     	;call command subroutine
	LCALL   lcddelay    	;give LCD some time
	MOV R0, #70h		;address of display message line 2
line2:
	CLR A
	MOV A, @R0		;moving the data to accumulator
	JZ delay_5s		;exit loop on completion of display amd call routine to wait 5 sec
	LCALL datawrt		;call data display routine
	LCALL lcddelay		;give LCD some time
	INC R0			;increment address
	SJMP line2		;repeat display till end of message
	
delay_5s:			;5 second delay routine
	MOV R7, #100		;REPEAT 100 TIMES (50ms*100= 5sec)
sec_5:
	MOV TL0, #0FDh		;load 4BFDh for 5ms delay
	MOV TH0, #4Bh
	SETB TR0		;Start timer 0
	JNB TF0, $		;Check for end of count
	CLR TR0			;Stop timer
	CLR TF0			;Clear timer 0 flag
	DJNZ R7, sec_5		;Repeat till R7=0
	CLR LCD_INTR		;Clear interrupt check flag
	MOV  A, #01h		;clear LCD
	LCALL comnwrt
	LCALL   lcddelay	;give LCD some time
	
sensor_display:		
	;DISPLAYING SENSOR 1: TEMP SENSOR
	
	MOV     A,#80H       	;cursor at line 1, position 0
   	LCALL   comnwrt     	;call command subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'T'       	;display letter T
    LCALL   datawrt       	;call display subroutine
	LCALL   lcddelay      	;give LCD some time
	MOV     A, #':'       	;display : symbol
   	LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV A, T_BUFFER			;move sensor data from buffer to accumulator
	MOV DPTR, #temp_tens	;address of ASCII code look up table (tens place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV A, T_BUFFER			;move sensor data from buffer to accumulator
	MOV DPTR, #temp_ones	;address of ASCII code look up table (ones place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV     A, #'.'       	;display . symbol
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
		
	MOV A, T_BUFFER			;move sensor data from buffer to accumulator
	MOV DPTR, #temp_dec		;address of ASCII code look up table (decimal place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV     A, #0DFh      	;display degree symbol 
    LCALL   datawrt       	;call display subroutine
    LCALL   lcddelay      	;give LCD some time
	MOV     A, #'C'       	;display letter C
    LCALL   datawrt       	;call display subroutine
	LCALL   lcddelay      	;give LCD some time
    MOV     A, #' '        	;display space
	LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time

	;DISPLAYING SENSOR 2: SOIL MOISTURE

	MOV     A, #'S'       	;display letter S
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'M'       	;display letter M
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #':'       	;display : symbol
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay		;give LCD some time
	
	MOV A, M_BUFFER		;move sensor data from buffer to accumulator
	CJNE A, #67h, dry
dry:
	JNC optimum
	MOV MOIST_LEVEL, #1	;Store level in a buffer (for threshold check)
	MOV 	A, #'D' 	;display D
	LCALL 	datawrt 	;call display subroutine
	LCALL 	lcddelay 	;give LCD some time
	MOV 	A, #'r' 	;display r
	LCALL 	datawrt 	;call display subroutine
	LCALL 	lcddelay 	;give LCD some time
	MOV 	A, #'y' 	;display y
	LCALL 	datawrt 	;call display subroutine
	LCALL 	lcddelay 	;give LCD some time
	MOV 	A, #' ' 	;display space
	LCALL 	datawrt 	;call display subroutine
	LCALL 	lcddelay 	;give LCD some time
	MOV 	A, #' ' 	;display space
	LCALL 	datawrt 	;call display subroutine
	LCALL 	lcddelay 	;give LCD some time
	
	SJMP moist_over
optimum:			;normal condition
	MOV A, M_BUFFER		;move sensor data from buffer to accumulator
	CJNE A, #78h, disp_opt
disp_opt:
	JNC slurry
	MOV MOIST_LEVEL, #2	;Store level in a buffer (for threshold check)
	MOV     A, #'O'        	;display O
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'p'        	;display p
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'t'        	;display t
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	SJMP moist_over

slurry:
	MOV MOIST_LEVEL, #3	;Store level in a buffer (for threshold check)
	MOV     A, #'E'        	;display E
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'x'        	;display x
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'c'        	;display c
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'e'        	;display e
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'s'        	;display s
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time

moist_over:
	
	;DISPLAYING SENSOR 3: RELATIVE HUMIDITY
	
	MOV     A,#0C0H       	;cursor at line 2, position 0
	LCALL   comnwrt     	;call command subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV     A, #'R'       	;display letter R
	LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'H'       	;display letter H
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #':'       	;display : symbol
	LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time

	MOV A, RH_BUFFER		;move sensor data from buffer to accumulator
	MOV DPTR, #rh_tens		;address of ASCII code look up table (tens place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV A, RH_BUFFER		;move sensor data from buffer to accumulator
	MOV DPTR, #rh_ones		;address of ASCII code look up table (ones place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV     A, #'.'       	;display . symbol
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	
	MOV A, RH_BUFFER		;move sensor data from buffer to accumulator
	MOV DPTR, #rh_dec		;address of ASCII code look up table (ones place)
	MOVC A, @A+DPTR			;store corresponding ASCII code to Accumulator
	LCALL   datawrt   		;call display subroutine
   	LCALL   lcddelay      	;give LCD some time

    MOV     A, #'%' 		;display % symbol
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	    	
 	;DISPLAYING SENSOR 4: LIGHT	

    MOV     A, #'L'       	;display letter L
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'I'       	;display letter I
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #':'       	;display : symbol
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time


	MOV A, L_BUFFER			;move sensor data from buffer to accumulator
	CJNE A, #25h, bright
bright:
	JNC normal
	MOV LIGHT_LEVEL, #1	;Store level in a buffer (for threshold check)
	MOV     A, #'O'        	;display O
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'p'        	;display p
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'t'        	;display t
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	LJMP light_over

normal:
	MOV A, L_BUFFER			;move sensor data from buffer to accumulator
	CJNE A, #82h, disp_norm
disp_norm:
	JNC dim
	MOV LIGHT_LEVEL, #2	;Store level in a buffer (for threshold check)
	MOV     A, #'D'        	;display D
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'i'        	;display i
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'m'        	;display m
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	SJMP light_over

dim:
	MOV A, L_BUFFER			;move sensor data from buffer to accumulator
	CJNE A, #9Dh, disp_dim
disp_dim:
	JNC dark
	MOV LIGHT_LEVEL, #3	;Store level in a buffer (for threshold check)
	MOV     A, #'D'        	;display D
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'a'        	;display a
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'r'        	;display r
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'k'        	;display space k
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #' '        	;display space
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	SJMP light_over

dark:
	MOV LIGHT_LEVEL, #4	;Store level in a buffer (for threshold check)
	MOV     A, #'N'        	;display N
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'i'        	;display i
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'g'        	;display g
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'h'        	;display space h
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time
	MOV     A, #'t'        	;display space t
    LCALL   datawrt       	;call display subroutine
   	LCALL   lcddelay      	;give LCD some time

light_over:
	RET


Threshold_check: 

temp_check:	;CHECKING TEMPERATURE THRESHOLD
	
	MOV A, T_BUFFER			;read sensor data from buffer
	CJNE A, #83, t_not_equal	;check for ambient temp (32deg C)
	SJMP t_within_thresh	;end of threshold checking routine

t_not_equal:
	JC t_within_thresh		;jump to within threshold routine
		
t_outside_thresh:
	SETB COOLER			;turn on heater
	JB BUZZ, no_buzz
	SETB BUZZER			;turn on buzzer
	ACALL delay
	CLR BUZZER			;turn off buzzer
	SETB BUZZ 			;set the buzzer flag high
	no_buzz:
	MOV 63h, #' '
	MOV 64h, #'O'
	MOV 65h, #'N'
	SJMP light_check
			
		
t_within_thresh:
	MOV A, T_BUFFER
	CJNE A,#73, ht_off
ht_off:	
	JNC light_check
	CLR COOLER			;turn off heater
	CLR BUZZ
	MOV 63h, #'O'
	MOV 64h, #'F'
	MOV 65h, #'F'	

Light_check:
			;CHECKING LIGHT THRESHOLDS
	MOV A, LIGHT_LEVEL
	CJNE A, #1, lev2
	CLR LIGHT1			;turn off light 1
	CLR LIGHT2			;turn off light 2
	MOV 7Bh, #'O'
	MOV 7Ch, #'F'
	MOV 7Dh, #'F'
	SJMP humid_check
lev2:
	MOV A, LIGHT_LEVEL
	CJNE A, #2, lev3
	SETB LIGHT1			;turn on light1
	CLR LIGHT2			;turn off light2
	JB BUZZ1, no_buzz1
	SETB BUZZER			;turn on buzzer
	ACALL delay
	CLR BUZZER			;turn off buzzer
	SETB BUZZ1
no_buzz1:	
	MOV 7Bh, #' '
	MOV 7Ch, #'O'
	MOV 7Dh, #'N'
 	SJMP humid_check
lev3:
	MOV A, LIGHT_LEVEL
	CJNE A, #3, lev4
	SETB LIGHT1			;turn on light1
	SETB LIGHT2			;turn on light2
	JB BUZZ1, no_buzz11
	SETB BUZZER			;turn on buzzer
	ACALL delay
	CLR BUZZER			;turn off buzzer
no_buzz11:
	MOV 7Bh, #' '
	MOV 7Ch, #'O'
	MOV 7Dh, #'N'
 	SJMP humid_check
lev4:
	CLR LIGHT1			;turn off light1
	CLR LIGHT2			;turn off light2
	CLR BUZZ1
	MOV 7Bh, #'O'
	MOV 7Ch, #'F'
	MOV 7Dh, #'F'

Humid_check:			;CHECKING RH THRESHOLD
	MOV A, RH_BUFFER		;read sensor data from buffer
	CJNE A, #40, h_not_equal	;check for ambient RH (correct this!!!!)
	SJMP h_within_thresh		;end of threshold checking routine
h_not_equal:
	JNC h_within_thresh		;jump to within threshold routine
		
h_outside_thresh:
	SETB SPRAYER		;turn on sprayer
	JB BUZZ2, no_buzz2
	SETB BUZZER			;turn on buzzer
	ACALL delay
	CLR BUZZER			;turn off buzzer
	SETB BUZZ
	no_buzz2:
	MOV 73h, #' '
	MOV 74h, #'O'
	MOV 75h, #'N'
	SJMP moist_check
			
h_within_thresh:
	CJNE A,#70, sp_off
sp_off:	JC moist_check
		
	CLR SPRAYER			;turn off sprayer
	MOV 73h, #'O'
	MOV 74h, #'F'
	MOV 75h, #'F'
	
Moist_check:
			;CHECKING MOISTURE THRESHOLDS
	MOV A, MOIST_LEVEL
	CJNE A, #1, level2
	SETB PUMP			;turn on pump
	JB BUZZ3, no_buzz3
	SETB BUZZER 		;turn on buzzer
	ACALL delay
	CLR BUZZER 			;turn off buzzer
	SETB BUZZ3
	no_buzz3:
	MOV 6Bh, #' '
	MOV 6Ch, #'O'
	MOV 6Dh, #'N'
	SJMP th_end_check
level2:
	MOV A, MOIST_LEVEL
	CJNE A, #2, level3
	CLR PUMP
	CLR BUZZ3
	MOV 6Bh, #'O'
	MOV 6Ch, #'F'
	MOV 6Dh, #'F'
	SJMP th_end_check
level3:
	CLR PUMP ;turn off pump
	CLR BUZZ3
	MOV 6Bh, #'O'
	MOV 6Ch, #'F'
	MOV 6Dh, #'F'

th_end_check:
end_check:	RET



int_isr: 			;interrupt routine for LCD button
	MOV IE, #00h	;Disable interrupts	
	PUSH 0E0h		;Push A to stack
	PUSH 00h		;Push R0
	PUSH 03h		;Push R3
	PUSH 04h		;Push R4
	PUSH 07h		;Push R7
	PUSH 82h		;Push DPL
	PUSH 83h		;Push DPH
	SETB LCD_INTR	;Set flag to check interrupt
	POP 83h			;Pop DPH
	POP 82h			;Pop DPL
	POP 07h			;Pop R7 from stack
	POP 04h			;Pop R4
	POP 03h			;Pop R3
	POP 00h			;Pop reg R0
	POP 0E0h		;Pop Accumulator
	
	MOV IE, #81h		;Enable INT0
	RETI
    	
		 
org 600h
init1: DB "   Welcome to   ", 0
init2: DB "Project GreenBee", 0
init3: DB "                ", 0
init4: DB "                ", 0
init5: DB "                ", 0
init6: DB "Initialising the", 0
init7: DB "Sensors.........", 0	; System initialisation message

temp_tens:
	DB '0','0','0','0','0','0','0','0','0','0'
    DB 30h, 30h, 30h, 30h, 30h, 30h, 30h, 30h, 30h, 30h
    DB 30h, 30h, 30h, 30h, 30h, 30h, 31h, 31h, 31h, 31h
	DB 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h
    DB 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h, 31h
	DB 31h, 31h, 32h, 32h, 32h, 32h, 32h, 32h, 32h, 32h
	DB 32h, 32h, 32h, 32h, 32h, 32h, 32h, 32h, 32h, 32h
	DB 32h, 32h, 32h, 32h, 32h, 32h, 32h, 32h, 33h, 33h
	DB 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h
	DB 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h, 33h
	DB 33h, 33h, 33h, 33h, 34h, 34h, 34h, 34h, 34h, 34h
	DB 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h
	DB 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h, 34h
	DB 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h
	DB 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h, 35h
	DB 35h, 35h, 35h, 35h, 35h, 35h, 36h, 36h, 36h, 36h
	DB 36h, 36h, 36h, 36h, 36h, 36h, 36h, 36h, 36h, 36h

 temp_ones:
 	DB '0', '0', '1', '1', '2', '2', '2', '3', '3', '3', '4'
	DB '4', '4', '5', '5', '5', '6', '6', '7', '7', '7'
	DB '8', '8', '8', '8', '9', '1', '1', '1', '2', '2'
	DB '3', '3', '4', '4', '5', '5', '5', '5', '5', '6'
	DB '6', '6', '7', '7', '7', '8', '8', '8', '9', '9'
	DB '9', '0', '0', '1', '1', '1', '2', '2', '2', '3'
	DB '3', '4', '4', '4', '5', '5', '6', '6', '6', '7'
	DB '7', '7', '8', '8', '9', '9', '9', '0', '0', '1'
	DB '1', '1', '2', '2', '2', '3', '3', '4', '4', '4'
	DB '5', '5', '5', '6', '6', '7', '7', '7', '8', '8'
	DB '9', '9', '9', '0', '0', '0', '1', '1', '2', '2'
	DB '2', '3', '3', '4', '4', '4', '5', '5', '6', '6'
	DB '6', '6', '7', '7', '0', '0', '0', '0', '0', '0'
	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'

temp_dec:
	DB '0', '0','0','0','0','5','5','5','5','5','0' ;10TH
	DB '0', '0','0','5','5','5','5','0','0','0'     ;20TH 
	DB '0', '5','5','5','5','0','0','0','0','0'     ;30TH 
	DB '5', '5','5','5','0','0','0','0','5','0'     ;40TH    
	DB '0', '5','0','5','0','5','0','5','0','0'     ;50TH
	DB '5', '0','5','0','5','5','0','5','5','0'     ;60TH
	DB '5', '0','0','5','0','5','0','0','5','0'     ;70TH
	DB '5', '5','0','5','0','0','5','0','5','0'     ;80TH   
	DB '0', '5','0','5','5','0','5','0','0','5'     ;90TH
	DB '0', '5','5','0','5','0','5','5','0','5'     ;100TH 
	DB '0', '0','5','0','5','5','0','5','0','0'     ;110TH
	DB '5', '5','0','0','5','5','0','0','5','5'     ;120TH
	DB '0', '5','0','5','5','5','0','0','5','5'     ;130TH
	DB '0', '0','0','0','0','0','0','0','0','0'     ;140TH	

 rh_tens:
 	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0','0' ;10th
	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'     ;20th
	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'     ;30th
	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'     ;40th
	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'     ;50th

	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'     ;60th
	DB '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'     ;70th
	DB '1', '1', '1', '2', '2', '2', '2', '2', '2', '2'     ;80th
	DB '2', '2', '2', '2', '2', '2', '2', '3', '3', '3'     ;90th
	DB '3', '3', '3', '3', '3', '3', '3', '3', '3', '3'     ;100th

	DB '3', '3', '3', '3', '4', '4', '4', '4', '4', '4'    ;110th
	DB '4', '4', '4', '4', '4', '4', '4', '4', '4', '5'    ;120th
	DB '5', '5', '5', '5', '5', '5', '5', '5', '5', '5'    ;130th
    DB '5', '5', '5', '5', '5', '6', '6', '6', '6', '6'		;140th
	DB '6', '6', '6', '6', '6', '6', '6', '6', '6', '6'		;150th
	DB '6', '7', '7', '7', '7', '7', '7', '7', '7', '7'		;160th
	DB '7', '7', '7', '7', '7', '7', '7', '8', '8', '8'		;170th
	DB '8', '8', '8', '8', '8', '8', '8', '8', '8', '9'		;180th
	DB '9', '9', '9', '9', '9', '9', '9', '9', '9', '9'		;190th
	DB '9', '9', '9', '9', '9', '9', '9', '9', '9', '9'		;200th

	DB '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'		;210th
	DB '0', '0', '0', '*', '*', '*', '*', '*', '*', '*'    ;220th 
	DB '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'    ;230th 
	DB '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'    ;240th
	DB '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'    ;250th
	DB '*', '*', '*', '*', '*' 								;250th


rh_ones:
	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0' ;10th
	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0'	  ;20th
	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0'	  ;30th
	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0'	  ;40th
	DB '1', '1', '2', '2', '3',  '3', '4', '4', '5', '5'	  ;50th
 
	DB '6', '7', '7', '8', '8',  '9', '0', '0', '1', '1'	;60th
	DB '3', '4', '4', '4', '5',  '5', '6', '6', '7', '8'	;70th
	DB '9', '9', '0', '1', '1',  '1', '2', '3', '4', '5'	;80th
	DB '5', '6', '7', '7', '7',  '8', '9', '0', '0', '0'	;90th
	DB '1', '2', '3', '3', '4',  '4', '5', '6', '6', '7'	;100th

	DB '8', '8', '8', '9', '0',  '1', '2', '2', '3', '3'	;110th
	DB '4', '4', '5', '6', '7',  '7', '8', '9', '9', '0'	;120th
	DB '0', '1', '1', '2', '3',  '3', '4', '5', '5', '6'	;130th
	DB '6', '7', '8', '9', '0',  '0', '0', '1', '1', '2'	;140th
	DB '3', '4', '4', '4', '5',  '6', '7', '7', '8', '8'	;150th
	
	DB '9', '9', '1', '1', '2',  '2', '3', '4', '4', '5'	;160th
	DB '6', '7', '7', '7', '5',  '8', '9', '0', '1', '1'	;170th
	DB '1', '2', '3', '4', '5',  '5', '6', '6', '7', '7'	;180th
	DB '8', '8', '9', '9', '0',  '1', '2', '3', '3', '4'	;190th
	DB '4', '4', '5', '6', '7',  '7', '8', '8', '9', '0'	;200th

	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;210th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;220th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;230th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;240th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;250th
	DB '*', '*', '*', '*', '*'				;256th

 rh_dec:
 	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0';10th
	DB '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0'	;20th
	DB '0', '0', '0', '1', '2',  '3', '4', '5', '6', '6'	;30th
	DB '7', '7', '8', '8', '8',  '9', '9', '9', '0', '0'	;40th
	DB '1', '1', '2', '2', '3',  '3', '4', '4', '5', '5'	;50th

	DB '6', '7', '7', '8', '8',  '8', '9', '9', '0', '0'	;60th
	DB '0', '1', '1', '1', '2',  '2', '3', '3', '3', '4'	;70th
	DB '5', '5', '5', '5', '6',  '6', '7', '1', '3', '5'	;80th
	DB '6', '0', '0', '6', '7',  '9', '2', '0', '8', '7'	;90th
	DB '6', '4', '3', '5', '1',  '9', '7', '5', '6', '3'	;100th

	DB '1', '9', '8', '7', '5',  '3', '2', '6', '0', '8'	;110th
	DB '6', '8', '4', '2', '0',  '7', '8', '6', '4', '3'	;120th
	DB '0', '1', '9', '7', '5',  '4', '3', '1', '9', '6'	;130th
	DB '7', '5', '4', '2', '0',  '0', '8', '6', '1', '4'	;140th
	DB '2', '0', '8', '4', '6',  '4', '3', '7', '1', '8'	;150th
	
	DB '7', '5', '1', '9', '7',  '9', '3', '7', '5', '9'	;160th
	DB '6', '4', '7', '7', '6',  '8', '4', '2', '5', '8'	;170th
	DB '9', '6', '5', '3', '5',  '4', '7', '6', '5', '7'	;180th
	DB '8', '6', '5', '3', '7',  '1', '2', '3', '5', '4'	;190th
	DB '0', '4', '5', '6', '2',  '7', '8', '8', '9', '0'	;200th

	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;210th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;220th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;230th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;240th
	DB '*', '*', '*', '*', '*',  '*', '*', '*', '*', '*'	;250th
	DB '*', '*', '*', '*', '*'				;256th
	
END